﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Optimus.Core
{
    public class MemoryManager
    {
       
        public static uint id = 0;

   
        public static void Init()
        {
            
        }

        public static void AddFreeBlock(uint Address, uint Size)
        {
            if (Cosmos.Core.Heap.AfterRun != false)
            {
                id = Cosmos.Core.Heap.hid;
                try
                {
                    Cosmos.Core.Heap.Blocks[id].Base = Address;
                    Cosmos.Core.Heap.Blocks[id].Size = Size;
                    Cosmos.Core.Heap.Blocks[id].Header = 0x1;
                    id++;
                }
                catch
                {
                    Console.WriteLine("Fatal Error!");
                    while (true) ;
                }
            }
        }

        public static void FreeMemory(uint id)
        {
            Cosmos.Core.Heap.Blocks[id].Header = 0;
            Cosmos.Core.Heap.FreeMem(Cosmos.Core.Heap.Blocks[id].Base, Cosmos.Core.Heap.Blocks[id].Size);
        }

        public static uint GetFreeMem()
        {
            uint addr = 0;
            for (int i = 0; i < id; i++)
            {
                if (Cosmos.Core.Heap.Blocks[i].Header == 0)
                {
                    addr = Cosmos.Core.Heap.Blocks[i].Base;
                    break;
                }
            }
            return addr;
        }
    }
}
